package com.akk.module.cjpay.service;
import com.akk.PayConstants;
import com.akk.module.cjpay.model.*;
import com.akk.module.cjpay.utils.ChanDePayUtil;
import com.akk.module.cjpay.utils.ChanPayUtil;
import com.akk.module.hlbpay.util.HlbBeanUtils;
import com.akk.utils.IDUtils;
import com.akk.utils.RestResponse;
import com.google.gson.Gson;
import org.apache.commons.httpclient.HttpStatus;
import org.junit.Test;
import org.springframework.stereotype.Component;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/*畅捷快捷支付*/
@Component
public class CjPayService {
    /**
     * 绑卡
     * @param request
     * @return
     */
    public RestResponse bindCard(CjpayBindCardRequest request){
        try {
            Map<String,String> params = HlbBeanUtils.convertBean(request,new LinkedHashMap());
            params.put("Version", "1.0");
            params.put("PartnerId", PayConstants.CJ_MER_NO);//200000400059 生产测试参数
            params.put("InputCharset", "UTF-8");// 字符集
            params.put("TradeDate", new SimpleDateFormat("yyyyMMdd").format(new Date()));// 商户请求时间
            params.put("TradeTime", new SimpleDateFormat("HHmmss").format(new Date()));// 商户请求时间
            params.put("Memo", "");
            params.put("Service",PayConstants.CJ_AUTH_BIND_CARD_SERVICE);
            System.out.println(new Gson().toJson(params));
            RestResponse restResponse = ChanPayUtil.gatewayPost(params,PayConstants.CJ_CHARSET,PayConstants.CJ_MERCHANT_PRIVATE_KEY);
//            if (restResponse.getCode().equals(String.valueOf(HttpStatus.SC_OK))){
//                CjpayBindCardResponse response = new Gson().fromJson(restResponse.getResult().toString(),CjpayBindCardResponse.class);
//                if (response.getStatus().equals("S") && response.getInstUrl() != null && response.getInstUrl().length() > 0){
//                    return RestResponse.build(response.getInstUrl());
//                }
//                return RestResponse.build(RestResponse.AVS_ERROR_CODE,response.getRetMsg());
//            }else {
//                return restResponse;
//            }
            if (restResponse.getCode().equals("1001")){
                return RestResponse.build(restResponse.getMessage());
            }else {
                return restResponse;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return RestResponse.build(RestResponse.AVS_ERROR_CODE,"绑卡失败");
    }
    @Test
    public void test(){
        CjpayBindCardRequest request = new CjpayBindCardRequest();
        request.setTrxId("201601236541233");
        request.setExpiredTime("90m");
        request.setMerUserId("zyr003");
        request.setNotifyUrl("http://dev.chanpay.com/receive.php");
        request.setReturnUrl("http://www.baidu.com");
        bindCard(request);
    }

    /**
     * 绑卡查询
     * @param request
     * @return
     */
    public RestResponse bindCardQuery(CJpayBindCardQueryRequest request){
        try {
            Map<String,String> params = HlbBeanUtils.convertBean(request,new LinkedHashMap());
            params.put("Version", "1.0");
            params.put("PartnerId", PayConstants.CJ_MER_NO);//200000400059 生产测试参数
            params.put("InputCharset", "UTF-8");// 字符集
            params.put("TradeDate", new SimpleDateFormat("yyyyMMdd").format(new Date()));// 商户请求时间
            params.put("TradeTime", new SimpleDateFormat("HHmmss").format(new Date()));// 商户请求时间
            params.put("Memo", "");
            params.put("Service",PayConstants.CJ_API_AUTH_INFO_QUERY);
            System.out.println(new Gson().toJson(params));
            RestResponse restResponse = ChanPayUtil.gatewayPost(params,PayConstants.CJ_CHARSET,PayConstants.CJ_MERCHANT_PRIVATE_KEY);
            if (restResponse.getCode().equals(String.valueOf(HttpStatus.SC_OK))){
                CjpayBindCardQueryResponse response = new Gson().fromJson(restResponse.getResult().toString(),CjpayBindCardQueryResponse.class);
                if (response.getStatus().equals("S")){
                    return restResponse;
                }else {
                    return RestResponse.build("2001",response.getRetMsg());
                }
            }else {
                return restResponse;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return RestResponse.build(RestResponse.AVS_ERROR_CODE,"绑卡失败");
    }

    @Test
    public void testQuery(){
        CJpayBindCardQueryRequest request = new CJpayBindCardQueryRequest();
        request.setTrxId(IDUtils.getUniqueNum14());
        request.setMerUserId("zyr003");
        request.setCardBegin("623156");
        request.setCardEnd("4912");
        RestResponse restResponse = bindCardQuery(request);
        System.out.println(new Gson().toJson(restResponse.getResult()));
    }

    /**
     * 直接支付
     * @param request
     * @return
     */
    public RestResponse directPayment(CjpayDirectPaymentRequest request){
        request.setSellerId(PayConstants.CJ_MER_NO);
        request.setExpiredTime("40m");
        request.setTradeType("11");
        request.setAccessChannel("web");
        request.setSubMerchantNo("");
        try {
            Map<String,String> params = HlbBeanUtils.convertBean(request,new LinkedHashMap());
            params.put("Version", "1.0");
            params.put("PartnerId", PayConstants.CJ_MER_NO);//200000400059 生产测试参数
            params.put("InputCharset", "UTF-8");// 字符集
            params.put("TradeDate", new SimpleDateFormat("yyyyMMdd").format(new Date()));// 商户请求时间
            params.put("TradeTime", new SimpleDateFormat("HHmmss").format(new Date()));// 商户请求时间
            params.put("Memo", null);
            params.put("Service",PayConstants.CJ_AUTH_QUICK_PAYMENT_SERVICE);
            RestResponse result = ChanPayUtil.gatewayPost(params,PayConstants.CJ_CHARSET,PayConstants.CJ_MERCHANT_PRIVATE_KEY);
            System.out.println(result.getResult());
            if (result.getCode().equals("1001")){
                return RestResponse.build(result.getMessage());
            } else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE,"支付失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return RestResponse.build(RestResponse.AVS_ERROR_CODE,"支付失败");
    }

    @Test
    public void test2(){
        CjpayDirectPaymentRequest request = new CjpayDirectPaymentRequest();
        request.setTrxId(Long.toString(System.currentTimeMillis()));
        request.setOrdrName("测试商品");
        request.setOrdrDesc("[{'商品型号':'D007','商品性能':'Test'}]");
        request.setMerUserId("1700000001");
        request.setSellerId(PayConstants.CJ_MER_NO);
        request.setSubMerchantNo("");
        request.setExpiredTime("40m");
        request.setTrxAmt("0.01");
        request.setTradeType("11");
        request.setAccessChannel("web");
        directPayment(request);
    }

    /**
     * 绑卡支付
     * @param request
     * @return
     */
    public RestResponse binkCardPayment(CjpayPaymentRequest request){
        try {
            Map<String,String> params = HlbBeanUtils.convertBean(request,new LinkedHashMap());
            params.put("Version", "1.0");
            params.put("PartnerId", PayConstants.CJ_MER_NO);//200000400059 生产测试参数
            params.put("InputCharset", "UTF-8");// 字符集
            params.put("TradeDate", new SimpleDateFormat("yyyyMMdd").format(new Date()));// 商户请求时间
            params.put("TradeTime", new SimpleDateFormat("HHmmss").format(new Date()));// 商户请求时间
            params.put("Memo", null);
            params.put("Service",PayConstants.CJ_AUTH_NQUICK_PAYMENT_SERVICE);
            RestResponse result = ChanPayUtil.buildRequest(params,"RSA",PayConstants.CJ_MERCHANT_PRIVATE_KEY,PayConstants.CJ_CHARSET,"https://pay.chanpay.com/mag-unify/gateway/receiveOrder.do?");
            if (result.getCode().equals(String.valueOf(HttpStatus.SC_OK))){
                return result;
            }else {
                return RestResponse.build(RestResponse.AVS_ERROR_CODE,"绑卡支付失败",result.getResult());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return RestResponse.build(RestResponse.AVS_ERROR_CODE,"支付失败");
    }

    // 畅捷代付
    public RestResponse deposit(CjPayDepositRequest request){
        request.setTransCode("T10000"); // 单笔同步代付
        try {
            Map<String,String> params = HlbBeanUtils.convertBean(request,new LinkedHashMap());
            params.put("Service","cjt_dsf");
            params.put("Version","1.0");
            params.put("PartnerId","200001160096");
            params.put("TradeDate",new SimpleDateFormat("yyyyMMdd").format(new Date()));
            params.put("TradeTime",new SimpleDateFormat("HHmmss").format(new Date()));
            params.put("InputCharset",PayConstants.CJ_CHARSET);
            params.put("Sign","");
            params.put("SignType","");
            String result =  ChanDePayUtil.sendPost(params,PayConstants.CJ_CHARSET,PayConstants.CJ_GATEWAY_MERCHANT_PRIVATE_KEY);
            if (result == null || result.length() == 0)
                return RestResponse.build(RestResponse.AVS_ERROR_CODE,"系统繁忙");
            else {
                CJPayDepositResponse response = new Gson().fromJson(result,CJPayDepositResponse.class);
                if (response.getAppRetcode().equals("00019999") && response.getOriginalRetCode().equals("000000")){
                    return RestResponse.build(response);
                }else {
                    return RestResponse.build(RestResponse.AVS_ERROR_CODE,response.getAppRetMsg());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestResponse.build(RestResponse.AVS_ERROR_CODE,"系统繁忙");
    }

    @Test
    public void test3(){
        CjpayPaymentRequest request = new CjpayPaymentRequest();
        request.setTrxId(Long.toString(System.currentTimeMillis()));
        request.setOrdrName("测试商品");
        request.setOrdrDesc("[{'商品型号':'D007','商品性能':'Test'}]");
        request.setMerUserId("1700000001");
        request.setSellerId(PayConstants.CJ_MER_NO);
        request.setSubMerchantNo("");
        request.setExpiredTime("40m");
        request.setTrxAmt("0.01");
        request.setTradeType("11");
        request.setAccessChannel("web");
        request.setCardBegin("635700");
        request.setCardEnd("7357");
        binkCardPayment(request);
    }

    @Test
    public void testDeposit(){
        CjPayDepositRequest request = new CjPayDepositRequest();
        request.setOutTradeNo(IDUtils.getUniqueNum14());
        request.setBusinessType("0");
        request.setBankCommonName("光大银行");
        request.setAcctNo(ChanPayUtil.encrypt("6231561600104912",PayConstants.CJ_GATEWAY_MERCHANT_PUBLIC_KEY,PayConstants.CJ_CHARSET));
        request.setAcctName(ChanPayUtil.encrypt("李正",PayConstants.CJ_GATEWAY_MERCHANT_PUBLIC_KEY,PayConstants.CJ_CHARSET));
        request.setTransAmt("0.10");
        deposit(request);
    }
}